package algorithm.hash;

import java.util.*;

public class WordGroup {
    public List<List<String>> groupAnagrams(String[] strs) {
        Map<String, List<String>> map = new HashMap<>();
        for (String str : strs) {
            char[] arr = str.toCharArray();
            Arrays.sort(arr);
            String key = String.valueOf(arr);
            List<String> list = map.get(key);
            if (list == null) {
                list = new ArrayList<>();
            }
            list.add(str);
            map.put(key, list);
        }
        List<List<String>> ans = new ArrayList<>(map.values());
        return ans;
    }
}
